<?php
/**
 * common.php
 * @author liuch
 * @version 1.0
 * @date 2017-08-03 9:56
 */
function saveOrder($data) {
    $fileName = ROOT_PATH . 'order.txt';
    $oldData = file_get_contents($fileName);
    $oldData = $oldData ? json_decode($oldData, true) : array();
    $data = array_merge($oldData, $data);
    $res = file_put_contents($fileName, json_encode(array_unique($data)));
    return FALSE !== $res;
}

function getOrder() {
    $fileName = ROOT_PATH . 'order.txt';
    $data = file_get_contents($fileName);
    $data = $data ? json_decode($data, true) : array();
    return $data;
}

function doneOrder($day) {
    $orders = getOrder();
    $orders = array_map(function($val) {
        return substr($val, 0, 10);
    }, $orders);
    return in_array($day, $orders);
}

function getRegisterDay($days) {
    $maxDay = date('Y-m-d', time() + 86400 * 14);
    $minDay = date('Y-m-d');
    $result = [];
    foreach ($days as $day) {
        if ($minDay <= $day and $day <= $maxDay and !doneOrder($day)) {
            $result[] = $day;
        }
    }
    return $result;
}

function refreshCookie() {
    require_once ROOT_PATH . 'Http.php';
    require_once ROOT_PATH . 'Cookie.php';
    require_once ROOT_PATH . 'Request.php';
    $http = new Http();
    $request = new Request();
    $cookieObj = new Cookie();
    $params = array(
        'userName' => 'MTg4NTA3MTczNTI=',
        'userPwd' => 'bG1qMTk5MDMyMjUyNg==',
        'securityCode' => '',
    );
    $http->curlGet($request->getUrl('Login', $params), $cookieObj->getCookie());
}